//
//  BlackNameVC.m
//  YaoHeAPP
//
//  Created by mac on 16/1/2.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "BlackNameVC.h"
#import "Controller.h"
#import "Masonry.h"
#import "BlackNameClass.h"
#import "CWStarRateView.h"
#import "UIImageView+WebCache.h"
#import "Define.h"
#import "SVPullToRefresh.h"
@interface BlackNameVC ()

@end

@implementation BlackNameVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"BlackNameVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"BlackNameVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"黑名单";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-50);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backUp) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.bottom.offset(-12);
    }];

    
    _dataArray=[[NSMutableArray alloc]init];
    
     [Controller requestWithURL:@"AppUserCenter/PostQueryBlackList?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
         if ([classData.number integerValue]==0) {
             NSMutableArray*ary=classData.data;
             if (ary.count==0) {
                 
                 UILabel*label=[[UILabel alloc]init];
                 label.text=@"加入黑名单的供货商将收不到你的求购信息,无法给你报价";
                 label.numberOfLines=0;
                 label.font=font15;
                 label.textAlignment=NSTextAlignmentCenter;
                 [self.view addSubview:label];
                 [label mas_makeConstraints:^(MASConstraintMaker *make) {
                     make.left.offset(20);  make.right.offset(-20);
                     make.top.equalTo(navcBar.mas_bottom).offset(20);
                 }];
                 
                 if (_tableView) {
                     [_tableView removeFromSuperview];
                 }
                 
                 return ;
             }
             
             for (NSInteger i=0; i<ary.count; i++) {
                 BlackNameClass*black=[[BlackNameClass alloc]initWith:ary[i]];
                 [_dataArray addObject:black];
             }
             
             if (_tableView) {
                 [_tableView removeFromSuperview];
             }

             _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, self.view.frame.size.width, self.view.frame.size.height-kNavigationBarHeight) style: UITableViewStylePlain];
             _tableView.dataSource=self;
             _tableView.delegate=self;
             _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
             _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小

             //_tableView.scrollEnabled=NO;
             [self.view addSubview:_tableView];
         }
         else{  ALERTSHOW(classData.message)  }
         __block BlackNameVC *vc = self;
         [_tableView addPullToRefreshWithActionHandler:^{
             //刷新数据的操作
             [vc viewDidLoad];
         }];

     }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

//每个分区单元格的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BlackNameClass*blackName=_dataArray[indexPath.row];
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
         cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
       
        //头像
        UIImageView*headImg=[[UIImageView alloc]init];
        headImg.frame=CGRectMake(15, 30, 50, 50);
        headImg.layer.masksToBounds=YES;
        headImg.layer.cornerRadius=25;
        headImg.tag=10;
        [cell addSubview:headImg];
        
        //公司名
        UILabel*label1=[[UILabel alloc]init];
        label1.font=font14;
        label1.textColor=[UIColor colorWithRed:102.f/255.f green:102.f/255.f blue:115.f/255.f alpha:1];
        label1.tag=11;
        [cell addSubview:label1];
        label1.frame=CGRectMake(70, 20, 200, 20);
//        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.offset(70);  make.height.offset(20);
//            make.top.offset(20);
//        }];
        //评分
        UILabel*starts=[[UILabel alloc]init];
//        starts.text=[NSString stringWithFormat:@"%@分",blackName.creditScore];
//        starts.textColor=[UIColor orangeColor];
//        starts.textAlignment=NSTextAlignmentRight;
//        starts.font=font14;
        [cell addSubview:starts];
//        [starts mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.offset(70);
//            make.height.offset(20);
//            make.top.offset(45);
//            make.width.offset(120);
//        }];
        starts.frame=CGRectMake(70, 45, 120, 20);
        
        
        //评分
        CWStarRateView*stars=[[CWStarRateView alloc]initWithFrame:CGRectMake(0,0, 80, 20) numberOfStars:5];
        stars.scorePercent=[blackName.creditScore floatValue]/5;
        stars.allowIncompleteStar = YES;
        stars.hasAnimation =YES;
        [starts addSubview:stars];
        UILabel*score=[[UILabel alloc]init];
        score.text=[NSString stringWithFormat:@"%.2f分",blackName.creditScore.floatValue];
        score.textColor=[UIColor orangeColor];
        score.textAlignment=NSTextAlignmentLeft;
        score.font=font14;
        [starts addSubview:score];
//        [score mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(stars.mas_right).offset(2);
//            make.top.offset(0);     make.bottom.offset(0);
//        }];
        
        score.frame=CGRectMake(100, 0, 50, 20);
        //抢单次数成功次数
        UILabel*label2=[[UILabel alloc]init];
        label2.textColor=[UIColor colorWithRed:102.f/255.f green:102.f/255.f blue:115.f/255.f alpha:1];
        label2.font=font12;
        label2.tag=13;
        [cell addSubview:label2];
//        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.offset(70); make.height.offset(20);make.top.offset(70);
//        }];
        label2.frame=CGRectMake(70, 70, 200, 20);
        
        UIImageView*lineImg1=[[UIImageView alloc]init];
        lineImg1.frame=CGRectMake(0, 109.5, self.view.frame.size.width, 0.5);
        lineImg1.backgroundColor=[UIColor lightGrayColor];
        [cell addSubview:lineImg1];
        
        
        
    }
    
    //NSString*string=@"123456";

    //头像
    UIImageView*headImage=(UIImageView*)[cell viewWithTag:10];
    [headImage sd_setImageWithURL:[NSURL URLWithString:blackName.logoPath] placeholderImage:[UIImage imageNamed:@"default_head"]];
    //公司名
    UILabel*companyName=(UILabel*)[cell viewWithTag:11];
    companyName.text=blackName.companyName;
    //评分
    CWStarRateView*starsScore=(CWStarRateView*)[cell viewWithTag:12];
    starsScore.scorePercent=[blackName.creditScore floatValue]/5 ;
    //抢单
    UILabel*GrabASingle=(UILabel*)[cell viewWithTag:13];
    GrabASingle.text=[NSString stringWithFormat:@"共抢单%@次  成功%@次",blackName.quoteNum,blackName.successNum];
    return cell;
}

////要求委托方的编辑风格在表视图的一个特定的位置。
//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
//    if ([tableView isEqual:_tableView]) {
//        result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
//    }
//    return result;
//}
//
//-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
//    [super setEditing:editing animated:animated];
//    [_tableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
//}

//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{//请求数据源提交的插入或删除指定行接收者。
//    if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
//        if (indexPath.row<[_dataArray count]) {
//            
//            BlackNameClass*blackName=_dataArray[indexPath.row];
//                    NSDictionary*dic=@{@"COMPANY_ID":blackName.companyID};
//                    [Controller requestWithURL:@"AppUserCenter/PostDeleteBlackList?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
//                        if ([classData.number integerValue]==0) {
//                            [_dataArray removeObjectAtIndex:indexPath.row];
//                            [_tableView reloadData];
//                        }
//                        else{
//                            ALERTSHOW(classData.message);
//                        }
//                    }];
//                }
//        
//    }
//}
//- (NSString*)tableView:(UITableView*)tableView
//titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
//    return @"删除";
//}


//要求委托方的编辑风格在表视图的一个特定的位置。
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
    if ([tableView isEqual:_tableView]) {
        result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
    }
    return result;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
}

- (NSString*)tableView:(UITableView*)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        [MobClick event:@"removeBlacklist"];
        
        BlackNameClass*blackName=_dataArray[indexPath.row];
        NSDictionary*dic=@{@"COMPANY_ID":blackName.companyID};
        [Controller requestWithURL:@"AppUserCenter/PostDeleteBlackList?from=app" jsondic:dic finishBlock:^(ClassData *classData) {
            if ([classData.number integerValue]==0) {
                [_dataArray removeObjectAtIndex:indexPath.row];
                [_tableView reloadData];
            }
            else{
                ALERTSHOW(classData.message);
            }
        }];
    }
}



//返回上个界面
-(void)backUp
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
